package com.cslg.ac.demo.dao.pet;


import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface LongAndLatDAO {
    String TABLE_NAME = "e_user_long_lat";
    String INSERT_FIELD = "user_id, longitude, latitude";
    String SELECT_FIELD = "id, user_id, longitude, latitude";

    @Insert({"insert into ",TABLE_NAME,"(", INSERT_FIELD, ") values(#{userId}, #{longitude},#{latitude})"})
    void insert(@Param("userId") int userId, @Param("longitude") double longitude, @Param("latitude") double latitude);

    @Select({"select user_id userId,(power(mod(abs(longitude - #{longitude}),360),2) + power(abs(latitude - #{latitude}),2)) as distance from", TABLE_NAME, "where longitude != '1000' and latitude != '1000' order by" +
            " distance limit #{offset}, #{num} "})
    List<Integer> userSortByLatAndLong(@Param("longitude") double longitude, @Param("latitude") double latitude, @Param("offset") int offset, @Param("num") int num);

    @Select({"select id from  ",TABLE_NAME, " where user_id = #{userId}"})
    Integer select(@Param("userId") int userId);

}
